package com.xnew.dict.controller;

import com.xnew.aes.LogParameter;
import com.xnew.bean.common.DictionaryInfo;
import com.xnew.dict.bean.EvaluateInfo;
import com.xnew.dict.service.DictCommonService;
import com.xnew.utils.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * @Author yangchengdong
 * @Date 2020/12/23
 */
@RestController
@RequestMapping("/dict")
public class DictCommonController {

    @Autowired
    private DictCommonService dictCommonService;

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
     * 数据字典单笔查询
     * */
    @RequestMapping(value = "/queryDictCommonResult", method = RequestMethod.POST)
    @ResponseBody
    public DataOutput queryDictCommonResult(@RequestBody DataInput input ,HttpServletRequest request ){
        DataOutput out = DataOutput.success();

        String type = input.getParam().get("type").toString();
        try{
            List<DictionaryInfo> list = dictCommonService.queryDictCommonResult(type);
            out.setData(list);
        }catch (Exception e){
            out.setCode(-90001);
            out.setMsg("数据字典查询出错");
        }
        return out;
    }

    /**
     * 数据字典批量查询
     * */
    @RequestMapping(value = "/queryDictCommonBatchResult", method = RequestMethod.POST)
    @ResponseBody
    public DataOutput queryDictCommonBatchResult(@RequestBody DataInput input){
        DataOutput out = DataOutput.success();
        String type = input.getParam().get("type").toString();
        try{
            Map<String, List<DictionaryInfo>> map = dictCommonService.queryDictCommonBatchResult(type);
            List<Map<String, List<DictionaryInfo>>> list = new ArrayList<Map<String, List<DictionaryInfo>>>();
            list.add(map);
            out.setData(list);
        }catch (Exception e){
            out.setCode(-80002);
            out.setMsg("数据字典批量查询出错");
        }
        return out;
    }

    /**
     * 业务编码生成通用方法
     * */
    @RequestMapping(value = "/buildBisinessCode", method = RequestMethod.POST)
    @ResponseBody
    public DataOutput buildBisinessCode(@RequestBody DataInput input){
        DataOutput out = DataOutput.success();
        String prefix = input.getParam().get("prefix").toString();
        try{
            String businessNo = IdUtils.buildBisinessCode(prefix);
            out.setData(businessNo);
        }catch (Exception e){
            out.setCode(-80003);
            out.setMsg("业务编码生成出错");
        }
        return out;
    }

    /**
     * 构建评价树通用方法
     * */
    @RequestMapping(value = "/buildEvaluateList", method = RequestMethod.POST)
    @ResponseBody
    @LogParameter(key = "evaluateType", desc = "查询评价信息项构建评价树")
    public DataOutput buildEvaluateList(@RequestBody DataInput input){
        DataOutput out = DataOutput.success();
        String evaluateType = input.getParam().get("evaluateType").toString();
        if(StringUtils.isEmpty(evaluateType)){
            out.setCode(-30000);
            out.setMsg("缺少必选参数");
            return out;
        }
        try{
            List<EvaluateInfo> list = dictCommonService.buildEvaluateList(evaluateType);
            out.setData(list);
        }catch (Exception e){
            out.setCode(-80008);
            out.setMsg("评价内容生成错误");
        }
        return out;
    }

    /**
     * 数据字典单笔查询
     * */
    @RequestMapping(value = "/queryDictTypeList", method = RequestMethod.POST)
    @ResponseBody
    public DataOutput queryDictTypeList(@RequestBody DataInput input){
        DataOutput out = DataOutput.success();
        try{
            List<Map<String, Object>> list = dictCommonService.queryDictTypeList(input);
            out = MybatisParam.getOutput(list);
            out.setData(list);
        }catch (Exception e){
            out.setCode(-90001);
            out.setMsg("数据字典查询出错");
        }
        return out;
    }

    /**
     * 数据字典新增
     * */
    @RequestMapping(value = "/saveDictTypeCommon", method = RequestMethod.POST)
    @ResponseBody
    public DataOutput saveDictTypeCommon(@RequestBody DataInput input){
        DataOutput out = DataOutput.success();
        String typeCode = input.getParam().get("typeCode").toString();
        try{
            List<Map<String, Object>> list = dictCommonService.queryDictType(typeCode);
            if(null != list && !list.isEmpty()){
                out.setCode(-90004);
                out.setMsg("字典类型已经存在");
                return out;
            }
            String typeName = input.getParam().get("typeName").toString();
            dictCommonService.saveDictTypeCommon(typeCode, typeName);
        }catch (Exception e){
            logger.error(e.getMessage(),e);
            out.setCode(-90001);
            out.setMsg("数据字典类型新增出错");
        }
        return out;
    }

    /**
     * 数据字典新增
     * */
    @RequestMapping(value = "/saveDictCodeCommon", method = RequestMethod.POST)
    @ResponseBody
    public DataOutput saveDictCodeCommon(@RequestBody DataInput input){
        DataOutput out = DataOutput.success();
        try{
            String type = input.getParam().get("type").toString();
            String code = input.getParam().get("code").toString();
            List<Map<String, Object>> list = dictCommonService.queryDictCodeBean(type, code);
            if(null != list && !list.isEmpty()){
                out.setCode(-90004);
                out.setMsg("字典已经存在");
                return out;
            }
            dictCommonService.saveDictCodeCommon(input.getParam());
        }catch (Exception e){
            logger.error(e.getMessage(),e);
            out.setCode(-90001);
            out.setMsg("数据字典新增出错");
        }
        return out;
    }

    /**
     * 机构人员初始化
     * */
    @RequestMapping(value = "/initOrgAndUserResult", method = RequestMethod.POST)
    @ResponseBody
    public void initOrgAndUserResult(HttpServletRequest request){
        try{
            dictCommonService.initOrgAndUserResult();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

//    /**
//     * 查询地区层级数据字典
//     * */
//    @RequestMapping(value = "/queryDictCommonRegion", method = RequestMethod.POST)
//    @ResponseBody
//    public DataOutput queryDictCommonRegion(@RequestBody DataInput input ,HttpServletRequest request ){
//        DataOutput out = DataOutput.success();
//
//        String type = input.getParam().get("type").toString();
//        try{
//            List<DictionaryInfo> list = dictCommonService.queryDictCommonRegion(type);
//            out.setData(list);
//        }catch (Exception e){
//            out.setCode(-90001);
//            out.setMsg("地区联级查询出错");
//        }
//        return out;
//    }
}
